home *** CD-ROM | disk | FTP | other *** search
- unit ODMenuU;
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, Menus, StdCtrls;
-
- type
- TForm1 = class(TForm)
- MainMenu1: TMainMenu;
- Menu1: TMenuItem;
- MenuItem1: TMenuItem;
- MenuItem2: TMenuItem;
- BmpDlg: TOpenDialog;
- Button1: TButton;
- HelpItem1: TMenuItem;
- About1: TMenuItem;
- HowtoUseHelp1: TMenuItem;
- SearchforHelpOn1: TMenuItem;
- Contents1: TMenuItem;
- procedure FormCreate(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- procedure Button1Click(Sender: TObject);
- procedure MenuItem1Click(Sender: TObject);
- procedure MenuItem2Click(Sender: TObject);
- procedure MenuItem3Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- Bmp: TBitmap;
- BmpFileName: String;
- procedure SetupMenus(const BmpName: String);
- procedure WMMeasureItem(var Msg: TWMMeasureItem); message wm_MeasureItem;
- procedure WMDrawItem(var Msg: TWMDrawItem); message wm_DrawItem;
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
-
- { Customise bitmaps at run-time. First is a bitmap menu, }
- { the second is fully owner draw, bitmap + its file name }
- procedure TForm1.SetupMenus(const BmpName: String);
- begin
- Bmp.LoadFromFile(BmpName);
- BmpFileName := BmpName;
- { First menu item is set to be a bitmap }
- ModifyMenu(Menu1.Handle, MenuItem1.Command,
- mf_ByCommand or mf_Bitmap, MenuItem1.Command, PChar(Bmp.Handle));
- { Second menu item is set to be an owner draw-menu }
- { So refer to the wm_MeasureItem and wm_DrawItem }
- { message handlers for the details }
- ModifyMenu(Menu1.Handle, MenuItem2.Command,
- mf_ByCommand or mf_OwnerDraw, MenuItem2.Command, PChar(Bmp));
- DrawMenuBar(Handle);
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- Bmp := TBitmap.Create;
- { Move Help menu over to the right }
- { Windows 95/NT 4 apps support doing it this way ... }
- {$ifndef AlternativeWay}
- ModifyMenu(MainMenu1.Handle, 1, mf_ByPosition or mf_Popup or mf_Help,
- HelpItem1.Handle, '&Help');
- {$else}
- { ... only Win 3.1x supports this }
- HelpItem1.Caption := #8 + HelpItem1.Caption;
- {$endif}
- { Start off with some bitmap }
- SetupMenus('c:\delphi\images\splash\16color\athena.bmp');
- end;
-
- procedure TForm1.FormDestroy(Sender: TObject);
- begin
- Bmp.Free;
- end;
-
- procedure TForm1.WMMeasureItem(var Msg: TWMMeasureItem);
- begin
- with Msg, MeasureItemStruct^ do
- if (IDCtl = 0) and (CtlType = odt_Menu) and
- (ItemID = MenuItem2.Command) then
- with TBitmap(ItemData), Canvas do
- begin
- { Width is bitmap width + text width }
- ItemWidth := Width + TextWidth(BmpFileName);
- { Height is Max(bitmap width, text width) }
- if Height > TextHeight(BmpFileName) then
- ItemHeight := Height
- else
- ItemHeight := TextHeight(BmpFileName);
- { Yes, we dealt with this message, all right? }
- LongBool(Result) := True;
- end;
- end;
-
- procedure TForm1.WMDrawItem(var Msg: TWMDrawItem);
- const
- Colors: array[False..True] of TColor = (clMenu, clHighlight);
- TextColors: array[False..True] of TColor = (clMenuText, clHighlightText);
- CopyModes: array[False..True] of TColor = (cmSrcCopy, cmNotSrcCopy);
- var
- Rect: TRect;
- SourceBmp: TBitmap;
- begin
- with Msg, DrawItemStruct^ do
- if (Ctl = 0) and (CtlType = odt_Menu) and
- (ItemID = MenuItem2.Command) and (HWndItem = Menu1.Handle) then
- with TCanvas.Create do
- try
- SourceBmp := TBitmap(ItemData);
- { Yes, we dealt with this message, all right? }
- LongBool(Result) := True;
- { Map TCanvas over menu DC }
- Handle := hDC;
- { Fill in background with appropriate colour }
- Brush.Color := Colors[ItemState and ods_Selected <> 0];
- FillRect(RCItem);
- { Centre bitmap vertically, in case shorter than the text }
- Rect := Bounds(RCItem.Left,
- (RCItem.Top + RCItem.Bottom - Bmp.Height) div 2,
- SourceBmp.Width, SourceBmp.Height);
- { Draw bitmap, normal or inverted as necessary }
- CopyMode := CopyModes[ItemState and ods_Selected <> 0];
- CopyRect(Rect, SourceBmp.Canvas, Classes.Rect(0, 0, SourceBmp.Width, SourceBmp.Height));
- { Set up same font as menus use }
- Font.Size := 8;
- Font.Color := TextColors[ItemState and ods_Selected <> 0];
- { Centre text vertically, in case shorter than the bitmap }
- TextOut(RCItem.Left + SourceBmp.Width,
- (RCItem.Top + RCItem.Bottom - TextHeight(BmpFileName)) div 2,
- BmpFileName);
- finally
- Free
- end;
- end;
-
- procedure TForm1.Button1Click(Sender: TObject);
- begin
- { Button allows menus to be dynamically }
- { changed by picking a new bitmap }
- if BmpDlg.Execute then
- SetUpMenus(BmpDlg.FileName);
- end;
-
- procedure TForm1.MenuItem1Click(Sender: TObject);
- begin
- ShowMessage('First menu item');
- end;
-
- procedure TForm1.MenuItem2Click(Sender: TObject);
- begin
- ShowMessage('Second menu item');
- end;
-
- procedure TForm1.MenuItem3Click(Sender: TObject);
- begin
- ShowMessage('Third menu item');
- end;
-
- end.
-